<?php

namespace src\controller;

use src\core\Database;
use src\models\Department;

class DepartmentCRUD
{
    private $department;

    public function __construct()
    {
//        error_log("DepartmentCRUD Controller Initialized");
        $db = new Database();
        $this->department = new Department($db->getConnection());
    }

    public function index()
    {
        $search = $_GET['search'] ?? '';
        $page = $_GET['page'] ?? 1;
        $limit = 5;
        $offset = ($page - 1) * $limit;

        // 构建查询条件
        $whereClause = '';
        $params = [];
        if (!empty($search)) {
            $whereClause = 'WHERE DepartmentName LIKE :name OR DepartmentDescription LIKE :description';
            $params = [
                ':name' => '%' . $search . '%',
                ':description' => '%' . $search . '%'
            ];
        }

        //获取资源
        // 获取分页数据（传递搜索条件）
        $departments = $this->department->getDepartment($limit, $offset, $whereClause, $params);
        //获取总行数（传递搜索条件）
        $total_rows = $this->department->getTotalRows($whereClause, $params);
        //计算总页数
        $total_pages = ceil($total_rows / $limit);
        //包含视图文件
        include __DIR__ . '/../views/department/department.view.php';
    }

    public function add()
    {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
//            $DepartmentName = $_POST['DepartmentName'];
//            $DepartmentDescription = $_POST['DepartmentDescription'];
            $name = filter_input(INPUT_POST, 'DepartmentName', FILTER_SANITIZE_STRING);
            $desc = filter_input(INPUT_POST, 'DepartmentDescription', FILTER_SANITIZE_STRING);

//            if ($this->department->addDepartment($DepartmentName, $DepartmentDescription)) {
            if ($this->department->addDepartment($name, $desc)) {
                header('Location: index.php?c=department&a=index');
                exit();
            }
        }
        include __DIR__ . '/../views/department/add.view.php';
    }

    public function edit()
    {
//        $id = $_GET['id'] ?? null;
        $id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
        if (!$id) die('ID 参数缺失');

        $department = $this->department->getDepartmentById($id);
        if (!$department) die('科室不存在');

        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
//            $DepartmentName = $_POST['DepartmentName'];
//            $DepartmentDescription = $_POST['DepartmentDescription'];
            $name = filter_input(INPUT_POST, 'DepartmentName', FILTER_SANITIZE_STRING);
            $desc = filter_input(INPUT_POST, 'DepartmentDescription', FILTER_SANITIZE_STRING);

//            if ($this->department->editDepartment($id, $DepartmentName, $DepartmentDescription)) {
            if ($this->department->editDepartment($id, $name, $desc)) {
                header('Location: index.php?c=department&a=index');
                exit();
            }
        }

        include __DIR__ . '/../views/department/edit.view.php';
    }

    public function delete()
    {
//        $id = $_GET['id'] ?? null;
        $id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);

        if (!$id) {
            die('ID 参数缺失');
        }

        if ($this->department->deleteDepartment($id)) {
            header('Location: index.php?c=department&a=index');
            exit;
        }
        die('删除失败');
    }
}